package com.carsystems.controller;

import com.carsystems.mapper.ViolationDetailsMapper;
import com.carsystems.pojo.ViolationDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ViolationDetailsController {
    @Autowired
    private ViolationDetailsMapper violationDetailsMapper;

    //查询全部的违规记录详情
    @GetMapping("/violationList")
    public List<ViolationDetails> violationList(){
        List<ViolationDetails> violationDetails = violationDetailsMapper.violationList();
        return  violationDetails;
    }

    //按violation_id查询单条违规记录详情
    @GetMapping("/violationid")
    public ViolationDetails violationid(int violation_id){
        return violationDetailsMapper.selectByPrimaryKey(violation_id);
    }

    //增加一条违规记录详情
    @ResponseBody
    @PutMapping("/addviolationdetails")
    public String addviolationdetails(@RequestBody ViolationDetails violationDetails){
        violationDetailsMapper.addviolationdetails(violationDetails);
       if (violationDetails != null){
           return "success";
       }else {
           return "fail";
       }
    }

    //修改一条违规记录详情
    @ResponseBody
    @PutMapping("/updateviolationdetails")
    public  String updateviolationdetails(@RequestBody ViolationDetails violationDetails){
        int row = violationDetailsMapper.updateviolationdetails(violationDetails);
        violationDetailsMapper.selectByPrimaryKey(violationDetails.getViolation_id());
        if (row == 1){
            return "success";
        }else {
            return "fail";
        }
    }

    //删除一条违规记录详情
    @ResponseBody
    @DeleteMapping("/deleviolationdetails")
    public String deleviolationdetails( ViolationDetails violationDetails){
       int row = violationDetailsMapper.deleviolationdetails(violationDetails.getViolation_id());
        if (row == 1){
            return "success";
        }else {
            return "fail";
        }
    }
}
